package com.gwtsz.chart.output.utils;

import java.io.Serializable;

/**
 * 绘制图表用到的时间，类型等数据
 *
 */
public enum Periodicity implements Serializable{
	M01(0, Periodicity.PERIOD_TYPE_MIN, 1, Periodicity.K_LINE_TYPE_1MIN),
	M05(1, Periodicity.PERIOD_TYPE_MIN, 5, Periodicity.K_LINE_TYPE_5MIN),
	M15(2, Periodicity.PERIOD_TYPE_MIN, 15, Periodicity.K_LINE_TYPE_15MIN),
	M30(3, Periodicity.PERIOD_TYPE_MIN, 30, Periodicity.K_LINE_TYPE_30MIN),
	H01(4, Periodicity.PERIOD_TYPE_HOUR, 1, Periodicity.K_LINE_TYPE_60MIN),
	H02(5, Periodicity.PERIOD_TYPE_HOUR, 2, Periodicity.K_LINE_TYPE_2HOUR),
	H04(6, Periodicity.PERIOD_TYPE_HOUR, 4, Periodicity.K_LINE_TYPE_4HOUR),
	D01(7, Periodicity.PERIOD_TYPE_DAILY, 0, Periodicity.K_LINE_TYPE_DAY),
	W01(8, Periodicity.PERIOD_TYPE_WEEK, 0, Periodicity.K_LINE_TYPE_WEEK),
	MON01(9, Periodicity.PERIOD_TYPE_MONTH, 0, Periodicity.K_LINE_TYPE_MONTH);

	/* C++ E_KLINE_TYPE */
	public static final int K_LINE_TYPE_5MIN = 0x01; // 5分钟K线
	public static final int K_LINE_TYPE_15MIN = 0x02; // 15分钟
	public static final int K_LINE_TYPE_30MIN = 0x03; // 30分钟K线
	public static final int K_LINE_TYPE_60MIN = 0x04; // 60分钟K线
	public static final int K_LINE_TYPE_DAY = 0x05; // 日K线
	public static final int K_LINE_TYPE_WEEK = 0x06; // 周K线
	public static final int K_LINE_TYPE_MONTH = 0x07; // 月K线
	public static final int K_LINE_TYPE_YEAR = 0x08; // 年K线
	public static final int K_LINE_TYPE_MANY_DAY = 15; // 多日线
	public static final int K_LINE_TYPE_MANY_MIN = 16; // 多分钟线
	public static final int K_LINE_TYPE_MANY_HOUR = 17; // 多小时线
	public static final int K_LINE_TYPE_1MIN = 35; // 1分钟
	public static final int K_LINE_TYPE_3MIN = 36; // 3分钟
	public static final int K_LINE_TYPE_2HOUR = 37; // 2小时
	public static final int K_LINE_TYPE_4HOUR = 38; // 4小时

	public static final byte PERIOD_TYPE_MIN = 1;
	public static final byte PERIOD_TYPE_HOUR = 2;
	public static final byte PERIOD_TYPE_DAILY = 3;
	public static final byte PERIOD_TYPE_WEEK = 4;
	public static final byte PERIOD_TYPE_MONTH = 5;
	public static final long MINUTE_IN_MS = 60000;
	public static final long HOUR_IN_MS = 60 * MINUTE_IN_MS;
	public static final long DAY_IN_MS = 24 * HOUR_IN_MS;
	public static final long WEEK_IN_MS = 7 * DAY_IN_MS;
	public static final long MONTH_IN_MS = 30 * DAY_IN_MS;

	/**标识周期在数组中的位置*/
	private int mPeriodId;
	private long mPeriodPerGrid;
	private byte mPeriodType;
	private byte mPeriodNum;
	private int mNativeKLineType;
	public static final int[] AvailableKLineTypes = { K_LINE_TYPE_1MIN, K_LINE_TYPE_5MIN, K_LINE_TYPE_15MIN, K_LINE_TYPE_30MIN, K_LINE_TYPE_60MIN, K_LINE_TYPE_2HOUR, K_LINE_TYPE_4HOUR, K_LINE_TYPE_DAY, K_LINE_TYPE_WEEK, K_LINE_TYPE_MONTH };
	public static final Periodicity[] AvailablePeriodicities = { M01, M05, M15, M30, H01, H02, H04, D01, W01, MON01 };

	public static int mapToAvailTypesIndex(int targetNativeType) {
		int i = 0;
		for (int nativeType : AvailableKLineTypes) {
			if (targetNativeType == nativeType)
				return i;

			i++;
		}
		return -1;
	}

	Periodicity(int periodID, int periodType, int periodNum, int nativeKLineType) {
		mPeriodId = periodID;
		mPeriodType = (byte) periodType;
		mPeriodNum = (byte) periodNum;
		mNativeKLineType = nativeKLineType;

		switch (periodType) {
		case PERIOD_TYPE_MIN:
			mPeriodPerGrid = mPeriodNum * MINUTE_IN_MS;
			break;
		case PERIOD_TYPE_HOUR:
			mPeriodPerGrid = mPeriodNum * HOUR_IN_MS;
			break;
		case PERIOD_TYPE_DAILY:
			mPeriodPerGrid = DAY_IN_MS;
			break;
		case PERIOD_TYPE_WEEK:
			mPeriodPerGrid = WEEK_IN_MS;
			break;
		case PERIOD_TYPE_MONTH:
			mPeriodPerGrid = MONTH_IN_MS;
			break;
		default:
			mPeriodPerGrid = 0;
			break;
		}
	}

	public static Periodicity nativeTypeToPeriodicity(int nativeKLineType) {
		for (Periodicity period : AvailablePeriodicities) {
			if (period.mNativeKLineType == nativeKLineType)
				return period;
		}
		return null;
	}

	public static Periodicity getDefault() {
		return D01;
	}

	/**返回周期在数组的位置*/
	public int periodID() {
		return mPeriodId;
	}

	public long periodPerGrid() {
		return mPeriodPerGrid;
	}

	public byte periodType() {
		return mPeriodType;
	}

	public byte periodNum() {
		return mPeriodNum;
	}

	public int nativeKLineType() {
		return mNativeKLineType;
	}

}
